using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using CodePlex.SpaceBlock;
using System.Reflection;
using CodePlex.SpaceBlock.UI.Controls;

namespace CodePlex.SpaceBlock.UI
{
    public class UIManager
    {
        private static UIManager _instance;

        public static UIManager Instance
        {
            get
            {
                return _instance;
            }

            set
            {
                _instance = value;
            }
        }

        private ExceptionForm _exceptionForm;
        private LogForm _logForm;
        private readonly ImageList _imageList;
        private readonly ToolStripLabel _statusBarLabel;

        public UIManager(ImageList smallImageList, ToolStripLabel statusBarLabel)
        {
            _imageList = smallImageList;
            _statusBarLabel = statusBarLabel;
        }

        public ExceptionForm ExceptionForm
        {
            get
            {
                if (_exceptionForm == null)
                {
                    _exceptionForm = new ExceptionForm();
                    _exceptionForm.Icon = this.GetIcon("Exception");
                }

                return _exceptionForm;
            }
        }


        public LogForm LogForm
        {
            get
            {
                if (_logForm == null)
                {
                    _logForm = new LogForm();
                }
                return _logForm;
            }
        }

        public void SetStatus(string message, params object[] parameters)
        {
            _statusBarLabel.Text = string.Format(message, parameters);
        }

        public ImageList SmallImageList
        {
            get 
            {
                return _imageList;
            }

        }

        public Icon GetIcon(string hint)
        {
            Image image = this.GetSmallImage(hint);

            return UIUtils.GetIcon(image);

        }

        public Image GetSmallImage(string hint)
        {
            if (this.SmallImageList.Images.Count == 0)  // why??
            {
                return null;
            }
            return this.SmallImageList.Images[this.GetSmallImageListIndex(hint)];
        }

        public int GetSmallImageListIndex(string hint)
        {

            Dictionary<string, int> map = new Dictionary<string, int>();
            map["ShowLog"] = 13;
            map["Options"] = 8;
            map["Exit"] = 7;
            map["About"] = 6;
            map["Properties"] = 18;
            map["EditACL"] = 4;
            map["EditLog"] = 14;
            map["Refresh"] = 17;
            map["Delete"] = 16;
            map["NewFolder"] = 15;
            map["ACLGroup"] = 4;
            map["ACLUser"] = 3;
            map["ACLPermission"] = 3;
            map["Question"] = 9;
            map["Exception"] = 9;
            map["Transfer"] = 12;
            map["File"] = 2;
            map["Folder"] = 0;
            map["Abstract"] = 19;
            map["S3"] = 20;
            map["Drive"] = 21;
            map["Add"] = 22;
            map["Remove"] = 23;
            map["Valid"] = 24;
            map["Invalid"] = 25;
            map["Key"] = 26;
            map["Url"] = 27;

            return map.ContainsKey(hint) ? map[hint] : 0;
           
        }

        public Image GetSmallImage(Path path, bool isOpen)
        {
            return this.SmallImageList.Images[this.GetSmallImageListIndex(path, isOpen)];
        }

        public int GetSmallImageListIndex(Path path, bool isOpen)
        {
            if (path.IsFolder)
            {
                return isOpen ? 1 : 0;
            }
            else
            {
                return 2;
            }
        }
    }
}
